package com.xspace.android.xafcommon.db;

import android.text.TextUtils;
import android.util.Log;

import com.xspace.android.imcommon.db.ImUserDb;
import com.xspace.android.imcommon.db.MucChatMessageDB;
import com.xspace.android.imcommon.db.RoomDB;
import com.xspace.android.imcommon.entity.ChatMsgSnap;
import com.xspace.android.imcommon.entity.MucChatMsgBean;
import com.xspace.android.imcommon.entity.MucRoomBean;
import com.xspace.android.xafcommon.appliction.BaseApplication;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;



public class MucMessageDBManager {
	MucChatMessageDB messageDB;
	RoomDB roomDB;
	ImUserDb imuserdb;
	static MucMessageDBManager manager;
    public static final String GroupHelperId="ChatClient_group_id";
    public static final String UnionCircleHelperId="ChatClient_Union_Circle_Id";
    /**圈子**/
    public static final String PlatForm_Circle="ChatClient_circle_xmpp";
    /**群组**/
    public static final String PlatForm_Group="ChatClient_group_xmpp";
	public MucMessageDBManager() {
		super();
		this.messageDB = new MucChatMessageDB(BaseApplication.mInstance.getApplicationContext());
		imuserdb=new ImUserDb(BaseApplication.mInstance.getApplicationContext());
		roomDB=new RoomDB(BaseApplication.mInstance.getApplicationContext());
	}

	public static MucMessageDBManager getInstance() {
		if(manager==null){
			manager=new MucMessageDBManager();
		}
		return manager;
	}
	public void insertOrUpdateMsg(MucChatMsgBean msg) {
		if(msg!=null){
		messageDB.insertOrUpdateMsg(msg);
		//插入会话列表消息显示
		insertOrUpdateSnapMsg(msg);
		}
	}
	public void insertOrUpdateMucMsg(MucChatMsgBean msg) {
		if(msg!=null){
		messageDB.insertOrUpdateMsg(msg);
		}
	}
	public void insertOrUpdateSnapMsg(MucChatMsgBean msg) {
		if(msg!=null){
			ChatMsgSnap chatmsgsnap=MucMsgToSnapMsg(msg);
			if(chatmsgsnap!=null){
				if (msg.mStatus == 3) {
					chatmsgsnap.mStatus = 3;
					if (msg.mRead == 0) {
						chatmsgsnap.mReadCount = 1;
					}
				}
				else{
					chatmsgsnap.mStatus = 1;
				}
		         int instag=	imuserdb.insertOrUpdateMsg(chatmsgsnap);
		         Log.v("mucdbmanger", "insertOrUpdateSnapMsg instag:"+instag+" snap contentType:"+chatmsgsnap.mLatestContentType);
			}
		}
	}
	/**
	 * 更新群组未读消息
	 */
	public void updateGroupListCount() {
//		imuserdb.doUpdateUnreadCount(GroupHelperId, MyInfoUtil.getMyBareJid(BaseApplication.mInstance), 0);
	}
	/**
	 * 更新联盟消息未读消息
	 */
	public void updateUnionCircleListCount() {
//	    imuserdb.doUpdateUnreadCount(UnionCircleHelperId, MyInfoUtil.getMyBareJid(BaseApplication.mInstance), 0);
	}
	public int queryUnreadCount(String groupid) {
		int count=0;
		if(!TextUtils.isEmpty(groupid)){
		count=messageDB.queryUnReadCount(groupid);
		}
		return count;
	}
	/**
	 * 
	 * @param groupid
	 * @param statu 0未读 1已读
	 */
	public void updateUnreadCount(String groupid ,int statu) {
		if(!TextUtils.isEmpty(groupid)){
			messageDB.updateMsgUnReadStatu(groupid ,statu);
			}

	}
	public void updateUnreadCount(String groupid ,int statu,int platform) {
		if(!TextUtils.isEmpty(groupid)){
			messageDB.updateMsgUnReadStatu(groupid ,statu);
			if(platform==0){//圈子时
//			imuserdb.doUpdateUnreadCount(groupid,MyInfoUtil.getMyBareJid(BaseApplication.mInstance), 0);
//			HuanXinManager.isNeedCheckCircleUnread=true;
			}
			else{
//			HuanXinManager.isNeedCheckGroupUnread=true;
			}
			}

	}
	private  ChatMsgSnap MucMsgToSnapMsg(MucChatMsgBean mucmsg) {
		 ChatMsgSnap msg=null;
		 if(mucmsg!=null){
			 int msg_plat=0;
			 String platform=mucmsg.mPlatForm;
			if (mucmsg.mStatus == 3) {
				if ("ChatClient_group".equals(platform)
						|| "ChatClient_group_xmpp".equals(platform)) {
					msg_plat = 2;
				} else if ("ChatClient_multi".equals(platform)
						|| "ChatClient_multi_xmpp".equals(platform)) {
					msg_plat = 3;
				} else if ("ChatClient_circle".equals(platform)
						|| "ChatClient_circle_xmpp".equals(platform)) {
					msg_plat = 1;
				}
			}
			 else if(mucmsg.mStatus<3){//自己发送的消息
				 if(mucmsg.mPlatForm!=null&&(TextUtils.isDigitsOnly(mucmsg.mPlatForm)))
				 msg_plat=Integer.parseInt(mucmsg.mPlatForm)+1;
			 }
			 if(msg_plat==3 || msg_plat==0) return null;
			 
			
			 msg=new ChatMsgSnap();
			 msg.mLatestContent=mucmsg.mContent;
			 msg.mLatestContentType=mucmsg.mContentType;
			 if( msg.mLatestContentType==3){
					try {
						JSONObject jb=new JSONObject(msg.mLatestContent);
						JSONArray jb1=jb.getJSONArray("newssnap");
						JSONObject jb2=jb1.getJSONObject(0);
						String title=jb2.getString("title");
						msg.mLatestContent=title;
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
			 msg.mLatestSendTime=mucmsg.mInsertTime;
			 msg.mBarejid=mucmsg.roomID;
			 msg.mToUserName=mucmsg.roomName;
//			 msg.mOwner=MyInfoUtil.getMyBareJid(BaseApplication.mInstance);
			 msg.msgType=msg_plat;
			 if(msg_plat==2) {
				 msg.mBarejid=GroupHelperId;
				 msg.mToUserName="群助手";
			 }
			 if(msg_plat==1) {
				 msg.mBarejid=UnionCircleHelperId;
				 msg.mToUserName="联盟消息";
			 }
		 }
       return msg;
	}
	/**
	 * 查询群助手或者联盟消息 暂时没有使用
	 * @param roomtype   圈子 1群聊 2多人会话 3xmpp 群组 4环信 圈子5环信 群组 6环信 多人会话 
	 */
	public void queryGroupHelper(int roomtype) {
		roomDB.queryGroupHelperList(roomtype);
	}
	/**
	 * 通过群组圈子消息记录查询圈子或群组
	 * @param paltform
	 * @return
	 */
	public ArrayList<MucRoomBean> queryRoomByPlatForm(String paltform) {
		return messageDB.queryRoomByPlatForm(paltform);

	}
	public ArrayList<MucChatMsgBean> queryAllRoomMsg() {
		return messageDB.queryAllRoomMsgs();

	}
}
